package com.zhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.domain.User;
import com.zhu.domain.enums.UserRoleEnum;
import com.zhu.mapper.UserMapper;
import com.zhu.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JoyBoy
 * @since 2022-05-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public boolean saveUser(User user) {
        return this.save(user);
    }

    @Override
//    @Cacheable(cacheNames = "user:getByName", key = "#userName", unless = "#result == null")
    public User getUserByUserName(String userName) {
        System.out.println(userName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        return this.getOne(queryWrapper);
    }

    @Override
//    @Cacheable(cacheNames="user::getById",key="#userId",unless="#result == null")
    public User getUserById(Integer userId) {
        return this.getById(userId);
    }

    @Override
    public User getUserByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return this.getOne(queryWrapper);
    }

    @Override
//    @CachePut(cacheNames="user::getById",key="#user.getUserId()",unless="#result == null")
    public boolean updateUserById(User user) {
        return this.updateById(user);
    }

    @Override
    public List<User> getAllUsers() {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_warehouse_manage", false);
        return this.list();
    }

    @Override
    public List<User> getUserByCondition(Integer userId, String userName, String realName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userId == null && userName == null && realName == null) {
            return this.list();
        } else {
            if (userId != null) {
                queryWrapper.like("user_id", userId);
            }
            if (!Objects.equals(userName, "")) {
                queryWrapper.like("user_name", userName);
            }
            if (!Objects.equals(realName, "")) {
                queryWrapper.like("real_name", realName);
            }
            return this.list(queryWrapper);
        }
    }

    @Override
//    @CacheEvict(cacheNames="user::getById",key="#userId")
    public boolean deleteUserById(Integer userId) {
        return this.removeById(userId);
    }


}
